<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 信息统计控制器
 */
class Statistics extends Admin_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('admin_log_model');
        $this->load->model('statistics_model');
    }

    function index(){
        redirect('statistics/loanstat');
    }
    //贷款统计
    function loanstat_old(){
        //分页
        $lwhere = "1 = 1";
        $where = "1 = 1";
        $search_type = $_GET['search_type']?intval(trim($_GET['search_type'])):1;
        $plat = $_GET['plat']?intval(trim($_GET['plat'])):0;
        $flag = $_GET['flag']?intval(trim($_GET['flag'])):0;
        $key = trim($_GET['key']);
        if ($key != '') {
            if ($search_type == 1) {
                $con = '平台中文名称';
                $lwhere .= " AND name_ch LIKE '%$key%' ";
            }
            if ($search_type == 2) {
                $con = '平台英文名称';
                $lwhere .= " AND name_en LIKE '%$key%' ";
            }
        }
        if ($plat){
            $where .= " and request_plat = $plat";
        }
        if ($flag){
            $where .= " and request_flag = $flag";
        }
        if (trim($_GET['time-start'])) {
            $time_start = strtotime($_GET['time-start']);
            $_time_start = $_GET['time-start'];
            $where .= " and request_time >= $time_start";
        }else{
            $_GET['time-start'] = date('Y-m-d',time());
            $time_start = strtotime($_GET['time-start']);
            $_time_start = $_GET['time-start'];
            $where .= " and request_time >= $time_start";
        }

        if (trim($_GET['time-end'])) {
            $time_end = strtotime($_GET['time-end']) + 3600 * 24;
            $_time_end = $_GET['time-end'];
            $where .= " and request_time <= $time_end";
        }else{
            $_GET['time-end'] = date('Y-m-d',time());
            $time_end = strtotime($_GET['time-end']) + 3600 * 24;
            $_time_end = $_GET['time-end'];
            $where .= " and request_time <= $time_end";
        }

        $lend_ids = "";
        $today = date('Y-m-d',time());
        if ($_time_start==$today || $_time_end==$today){
            $today_lendIds = $this->db->query("select id from tan_lend_list WHERE status = 1 and is_mobile=1 ORDER by id asc")->result_array();
            foreach ($today_lendIds as $today_item=>$today_val){
                $lend_ids .= $today_val['id'] . ',';
            }
        }

        $lend_log = $this->db->query("select * from tan_lend_day_log WHERE time_date>='$_time_start' AND time_date<='$_time_end' ORDER by id DESC")->result_array();
        if ($lend_log) {
            foreach ($lend_log as $item => $day_val) {
                $lend_ids .= $day_val['lend_id'] . ',';
            }
        }
        if ($lend_ids) {
            $lend_idArr = explode(',', trim($lend_ids, ','));
            if ($lend_idArr) {
                array_multisort($lend_idArr, SORT_ASC, $lend_idArr);
                $lend_idArr = array_unique($lend_idArr);
                $lwhere .= " AND id IN(" . implode(',', $lend_idArr) . ") ";
            }
        }

        $getcount = $this->db->query("select * from tan_lend_list WHERE $lwhere AND status = 1 and is_mobile=1 order by id desc")->num_rows();
//        $navigation_position = 'outside';
//        $records_per_page = 50;
//        $this->xpage->navigation_position(isset($navigation_position) && in_array($navigation_position, array('left', 'right')) ? $navigation_position : 'outside');
//        //$this->xpage->reverse(true);
//        $this->xpage->records($getcount);
//        $this->xpage->records_per_page($records_per_page);
//        $this->xpage->labels("<<", ">>");

//        $lend_list = $this->db->query("select id,title,name_ch,name_en from tan_lend_list WHERE $lwhere AND is_mobile=1 order by id desc limit ".(($this->xpage->get_page() - 1) * $records_per_page).",".$records_per_page)->result_array();
        $lend_list = $this->db->query("select id,title,name_ch,name_en from tan_lend_list WHERE $lwhere AND is_mobile=1 order by sort desc")->result_array();

        $list['index_views'] = $this->db->query("SELECT request_ip FROM tan_reload_log WHERE $where AND type_val=0 AND type=2")->num_rows();
        $list['index_views_removal'] = $this->db->query("SELECT request_ip FROM tan_reload_log WHERE $where AND type_val=0 AND type=2 GROUP BY request_ip")->num_rows();
//        $list['index_jump'] = $this->db->query("SELECT request_ip FROM tan_reload_log WHERE $where AND type_val=0 AND type=1")->num_rows();
//        $list['index_jump_removal'] = $this->db->query("SELECT request_ip FROM tan_reload_log WHERE $where AND type_val=0 AND type=1 GROUP BY request_ip")->num_rows();
        $list['index_jump'] = 0;
        $list['index_jump_removal'] = 0;

        $list['all_views_removal'] = 0;
        $list['all_jump_removal'] = 0;

        $list['all_views'] = $this->db->query("SELECT request_ip FROM tan_reload_log WHERE $where AND type_val>0 AND type=2")->num_rows();
//        $list['all_views_removal'] = $this->db->query("SELECT request_ip FROM tan_reload_log WHERE $where AND type_val>0 AND type=2 GROUP BY request_ip")->num_rows();
        $list['all_jump'] = $this->db->query("SELECT request_ip FROM tan_reload_log WHERE $where AND type_val>0 AND type=1")->num_rows();
        $list['_all_jump_removal'] = $this->db->query("SELECT request_ip FROM tan_reload_log WHERE $where AND type_val>0 AND type=1 GROUP BY request_ip")->num_rows();
        $list['all_views_removal'] = $list['_all_jump_removal'];
        foreach ($lend_list as $lend_item=>$lend){

            $lend_list[$lend_item]['views_num'] = $this->db->query("SELECT request_ip FROM tan_reload_log WHERE $where AND type_val=".$lend['id']." AND type=2")->num_rows();
            $lend_list[$lend_item]['views_num_removal'] = $this->db->query("SELECT request_ip FROM tan_reload_log WHERE $where AND type_val=".$lend['id']." AND type=2 GROUP BY request_ip")->num_rows();

            $lend_list[$lend_item]['jump_num'] = $this->db->query("SELECT request_ip FROM tan_reload_log WHERE $where AND type_val=".$lend['id']." AND type=1")->num_rows();
            $lend_list[$lend_item]['jump_num_removal'] = $this->db->query("SELECT request_ip FROM tan_reload_log WHERE $where AND type_val=".$lend['id']." AND type=1 GROUP BY request_ip")->num_rows();
//            $list['all_views_removal'] += $lend_list[$lend_item]['views_num_removal'];
            $list['all_jump_removal'] += $lend_list[$lend_item]['jump_num_removal'];
        }
        $list['lend_list'] = $lend_list;

        $plat_list = $this->db->query("SELECT * FROM tan_dictionary WHERE type=1")->result_array();
        $flag_list = $this->db->query("SELECT * FROM tan_dictionary WHERE type=2")->result_array();

        if ($plat){
            $plat_ = $this->db->query("SELECT value,name FROM tan_dictionary WHERE type=1 AND value=$plat")->row_array();
            $this->template->assign('plat_name', $plat_['name']);
        }
        if ($flag){
            $flag_ = $this->db->query("SELECT value,name FROM tan_dictionary WHERE type=2 AND value=$flag")->row_array();
            $this->template->assign('flag_name', $flag_['name']);
        }

        $this->template->assign('plat_list', $plat_list);
        $this->template->assign('flag_list', $flag_list);

        $this->template->assign('stname', $search_type);
        $this->template->assign('keyname', trim($_GET['key']));
        $this->template->assign('conname', $con);
        $this->template->assign('time_start', trim($_GET['time-start']));
        $this->template->assign('time_end', trim($_GET['time-end']));
        $this->template->assign('Total',$getcount);
        $this->template->assign('showpage',$this->xpage->render(true));
        $this->template->assign('list',$list);
        $this->template->load('statistics/loanstat');
    }
    function loanstat(){
        //分页
        $lwhere = " ";
        $where = "1 = 1";
        $search_type = $_GET['search_type']?intval(trim($_GET['search_type'])):1;
        $plat = $_GET['plat']?intval(trim($_GET['plat'])):0;
        $flag = $_GET['flag']?intval(trim($_GET['flag'])):0;
        $key = trim($_GET['key']);
        if ($key != '') {
            if ($search_type == 1) {
                $con = '平台中文名称';
                $lwhere .= " AND ll.name_ch LIKE '%$key%' ";
            }
            if ($search_type == 2) {
                $con = '平台英文名称';
                $lwhere .= " AND ll.name_en LIKE '%$key%' ";
            }
        }
        if ($plat){
            $where .= " and rl.request_plat = $plat";
        }
        if ($flag){
            $where .= " and rl.request_flag = $flag";
        }
        if (trim($_GET['time-start'])) {
            $time_start = strtotime($_GET['time-start']);
            $_time_start = $_GET['time-start'];
            $where .= " and rl.request_time >= $time_start";
        }else{
            $_GET['time-start'] = date('Y-m-d',time());
            $time_start = strtotime($_GET['time-start']);
            $_time_start = $_GET['time-start'];
            $where .= " and rl.request_time >= $time_start";
        }

        if (trim($_GET['time-end'])) {
            $time_end = strtotime($_GET['time-end']) + 3600 * 24;
            $_time_end = $_GET['time-end'];
            $where .= " and rl.request_time <= $time_end";
        }else{
            $_GET['time-end'] = date('Y-m-d',time());
            $time_end = strtotime($_GET['time-end']) + 3600 * 24;
            $_time_end = $_GET['time-end'];
            $where .= " and rl.request_time <= $time_end";
        }
//        var_dump("SELECT DISTINCT rl.type_val,IF(rl.type_val=0,'首页',ll.name_ch) AS name_ch,IF(rl.type_val=0,'首页',ll.name_en) AS name_en FROM `tan_reload_log` rl LEFT JOIN `tan_lend_list` ll ON ll.id=rl.type_val WHERE $where $lwhere");
        $getcount = $this->db->query("SELECT DISTINCT rl.type_val FROM `tan_reload_log` rl LEFT JOIN `tan_lend_list` ll ON ll.id=rl.type_val WHERE $where $lwhere AND rl.type_val>0 AND rl.type IN(1,2)")->num_rows();
        $lend_arr = $this->db->query("SELECT DISTINCT rl.type_val,IF(rl.type_val=0,'首页',ll.name_ch) AS name_ch,IF(rl.type_val=0,'首页',ll.name_en) AS name_en FROM `tan_reload_log` rl LEFT JOIN `tan_lend_list` ll ON ll.id=rl.type_val WHERE $where $lwhere AND rl.type IN(1,2)")->result_array();
        $views_log_arr = $this->db->query("SELECT type_val,COUNT(request_ip) AS num,COUNT(DISTINCT request_ip) AS rnum FROM tan_reload_log rl WHERE $where AND rl.type=2  GROUP BY type_val ORDER BY num DESC")->result_array();
        $jump_log_arr = $this->db->query("SELECT type_val,COUNT(request_ip) AS num,COUNT(DISTINCT request_ip) AS rnum FROM tan_reload_log rl WHERE $where AND rl.type=1 GROUP BY type_val ORDER BY num DESC")->result_array();
        $list = array();
        $lend_list = array();
        $list['index_views'] = 0;
        $list['index_views_removal'] = 0;
        $list['all_views_removal'] = 0;
        $list['all_jump_removal'] = 0;

        $list['all_views'] = $this->db->query("SELECT rl.request_ip FROM tan_reload_log rl WHERE $where AND rl.type_val>0 AND rl.type=2")->num_rows();
//        $list['all_views_removal'] = $this->db->query("SELECT request_ip FROM tan_reload_log WHERE $where AND type_val>0 AND type=2 GROUP BY request_ip")->num_rows();
        $list['all_jump'] = $this->db->query("SELECT rl.request_ip FROM tan_reload_log rl WHERE $where AND rl.type_val>0 AND rl.type=1")->num_rows();

        $list['_all_jump_removal'] = $this->db->query("SELECT rl.request_ip FROM tan_reload_log rl WHERE $where AND type_val>0 AND type=1 GROUP BY request_ip")->num_rows();
        $list['all_views_removal'] = $list['_all_jump_removal'];
        foreach ($lend_arr as $lend_item=>$lend_val){
            if ($lend_val['type_val']<>0){
                $lend_list[$lend_item]['name_ch'] = $lend_val['name_ch'];
                $lend_list[$lend_item]['id'] = $lend_val['type_val'];
                $lend_list[$lend_item]['views_num'] = 0;
                $lend_list[$lend_item]['views_num_removal'] = 0;
                $lend_list[$lend_item]['jump_num'] = 0;
                $lend_list[$lend_item]['jump_num_removal'] = 0;
            }

            foreach ($views_log_arr as $log_item=>$log_val){
                if ($log_val['type_val']==$lend_val['type_val']){
                    if ($lend_val['type_val']==0){
                        $list['index_views'] = $log_val['num'];
                        $list['index_views_removal'] = $log_val['rnum'];
                    }else{
                        $lend_list[$lend_item]['views_num']=$log_val['num'];
                        $lend_list[$lend_item]['views_num_removal']=$log_val['rnum'];
                    }
                }
            }
            foreach ($jump_log_arr as $jump_item=>$jump_val){
                if ($jump_val['type_val']==$lend_val['type_val']){
                    $lend_list[$lend_item]['jump_num']=$jump_val['num'];
                    $lend_list[$lend_item]['jump_num_removal']=$jump_val['rnum'];
                }
            }
            $list['all_jump_removal'] += $lend_list[$lend_item]['jump_num_removal'];
        }
        $list['lend_list'] = $this->sortArrByField($lend_list,'views_num',true);

        $plat_list = $this->db->query("SELECT * FROM tan_dictionary WHERE type=1")->result_array();
        $flag_list = $this->db->query("SELECT * FROM tan_dictionary WHERE type=2")->result_array();

        if ($plat){
            $plat_ = $this->db->query("SELECT value,name FROM tan_dictionary WHERE type=1 AND value=$plat")->row_array();
            $this->template->assign('plat_name', $plat_['name']);
        }
        if ($flag){
            $flag_ = $this->db->query("SELECT value,name FROM tan_dictionary WHERE type=2 AND value=$flag")->row_array();
            $this->template->assign('flag_name', $flag_['name']);
        }

        $this->template->assign('plat_list', $plat_list);
        $this->template->assign('flag_list', $flag_list);

        $this->template->assign('stname', $search_type);
        $this->template->assign('keyname', trim($_GET['key']));
        $this->template->assign('conname', $con);
        $this->template->assign('time_start', trim($_GET['time-start']));
        $this->template->assign('time_end', trim($_GET['time-end']));
        $this->template->assign('Total',$getcount);
        $this->template->assign('showpage',$this->xpage->render(true));
        $this->template->assign('list',$list);
        $this->template->load('statistics/loanstat');
    }
    //统计图表
    function loanstat_chart(){
        //分页
        $lend_id = trim($_GET['lend_id']);
        $plat = $_GET['plat']?intval($_GET['plat']):0;
        $flag = $_GET['flag']?intval($_GET['flag']):0;

        if ($_POST){
            $where = " 1=1 ";
            if (trim($_POST['time_start'])) {
                $time_start = strtotime($_POST['time_start']);
                $where .= " AND request_time >= $time_start";
            }else{
                $_POST['time_start'] = date('Y-m-d',time()-(6*24*3600));
                $time_start = strtotime($_POST['time-start']);
                $where .= " AND request_time >= $time_start";
            }

            if (trim($_POST['time_end'])) {
                $time_end = strtotime($_POST['time_end']) + 3600 * 24;
                $where .= " AND request_time <= $time_end";
            }else{
                $_POST['time_end'] = date('Y-m-d',time());
                $time_end = strtotime($_POST['time_end']) + 3600 * 24;
                $where .= " AND request_time <= $time_end";
            }

            if (trim($_POST['plat'])){
                $plat = intval(trim($_POST['plat']));
                $where .= " AND request_plat = $plat";
            }
            if (trim($_POST['flag'])){
                $flag = intval(trim($_POST['flag']));
                $where .= " AND request_flag = $flag";
            }

            $title['text'] = "总二级页-图表";
            if ($lend_id >= 0){
                $where .= " AND type_val = $lend_id";
                if ($lend_id > 0){
                    $lend_ = $this->db->query("SELECT name_ch from tan_lend_list WHERE id=$lend_id")->row_array();
                    $title['text'] = $lend_['name_ch'].'-图表';
                }else{
                    $title['text'] = "首页-图表";
                }
            }else{
                $where .= " AND type_val>0 ";
            }

            $viewnums = $this->db->query("SELECT COUNT(*) AS num, FROM_UNIXTIME(request_time,'%Y-%m-%d') AS days FROM tan_reload_log WHERE $where AND type=2 GROUP BY days")->result_array();
            $viewnums_removal = $this->db->query("SELECT COUNT(DISTINCT request_ip) AS num, FROM_UNIXTIME(request_time,'%Y-%m-%d') AS days FROM tan_reload_log WHERE $where AND type=2 GROUP BY days")->result_array();
            $jumpnums = $this->db->query("SELECT COUNT(*) AS num, FROM_UNIXTIME(request_time,'%Y-%m-%d') AS days FROM tan_reload_log WHERE $where AND type=1 GROUP BY days")->result_array();
            $jumpnums_removal = $this->db->query("SELECT COUNT(DISTINCT request_ip,type_val) AS num, FROM_UNIXTIME(request_time,'%Y-%m-%d') AS days FROM tan_reload_log WHERE $where AND type=1 GROUP BY days")->result_array();

            if ($time_start<$time_end){
                $cout_day = ($time_end - $time_start)/86400;
            }else{
                $cout_day = 7;
            }

            $days = array();
            for($i=$cout_day; $i>0; $i--){
                $days[] = date('Y-m-d',$time_end-3600 * 24 * $i);
            }

            $viewnums_arr = array();
            $viewnums_removal_arr = array();
            $jumpnums_arr = array();
            $jumpnums_removal_arr = array();
            foreach ($days as $daykey=>$day){
                if (!$this->deep_in_array($day,$viewnums)){
                    $viewnums[] = array('days'=>$day,'num'=>0);
                }
                if (!$this->deep_in_array($day,$viewnums_removal)){
                    $viewnums_removal[] = array('days'=>$day,'num'=>0);
                }
                if (!$this->deep_in_array($day,$jumpnums)){
                    $jumpnums[] = array('days'=>$day,'num'=>0);
                }
                if (!$this->deep_in_array($day,$jumpnums_removal)){
                    $jumpnums_removal[] = array('days'=>$day,'num'=>0);
                }
            }
//            array_multisort(array_column($viewnums,'days'),SORT_ASC,$viewnums);
//            array_multisort(array_column($viewnums_removal,'days'),SORT_ASC,$viewnums_removal);
//            array_multisort(array_column($jumpnums,'days'),SORT_ASC,$jumpnums);
//            array_multisort(array_column($jumpnums_removal,'days'),SORT_ASC,$jumpnums_removal);

            $viewnums = $this->sortArrByField($viewnums,'days');
            $viewnums_removal = $this->sortArrByField($viewnums_removal,'days');
            $jumpnums = $this->sortArrByField($jumpnums,'days');
            $jumpnums_removal = $this->sortArrByField($jumpnums_removal,'days');

            foreach ($viewnums as $vitem=>$nval){
                $viewnums_arr[] = $nval['num'];
            }
            foreach ($viewnums_removal as $vritem=>$vrval){
                $viewnums_removal_arr[] = $vrval['num'];
            }
            foreach ($jumpnums as $jntem=>$jnval){
                $jumpnums_arr[] = $jnval['num'];
            }
            foreach ($jumpnums_removal as $jrtem=>$jrval){
                $jumpnums_removal_arr[] = $jrval['num'];
            }

            $option = array(
                'title'     => $title,
                'tooltip'   => array(
                    'trigger'  => 'axis',
                    'axisPointer'=> array(
                        'type'  => 'cross',
                        'label' => array(
                            'backgroundColor'=>'#6a7985'
                        )
                    )
                ),
                'legend'    => array(
                    'data'  => ['浏览量','去重浏览量','跳转量','去重跳转量']
                ),
                'yAxis'   => [array('type'=>'value')],
                'xAxis'   => [array('type'=>'category','boundaryGap'=>false,'data'=>$days)],
                'series'  => [
                    array(
                        'name'  => '浏览量',
                        'type'  => 'line',
                        'stack'  => '总量',
                        'areaStyle'  => array('normal'=>array()),
                        'data'  => $viewnums_arr,
                    ),
                    array(
                        'name'  => '去重浏览量',
                        'type'  => 'line',
                        'stack'  => '总量',
                        'areaStyle'  => array('normal'=>array()),
                        'data'  => $viewnums_removal_arr,
                    ),
                    array(
                        'name'  => '跳转量',
                        'type'  => 'line',
                        'stack'  => '总量',
                        'areaStyle'  => array('normal'=>array()),
                        'data'  => $jumpnums_arr,
                    ),
                    array(
                        'name'  => '去重跳转量',
                        'type'  => 'line',
                        'stack'  => '总量',
                        'areaStyle'  => array('normal'=>array()),
                        'data'  => $jumpnums_removal_arr,
                    ),
                ]

            );
            echo json_encode(array('option'=>$option),JSON_HEX_TAG);die;
        }else{
            $plat_list = $this->db->query("SELECT * FROM tan_dictionary WHERE type=1")->result_array();
            $this->template->assign('plat_list', $plat_list);

            $flag_list = $this->db->query("SELECT * FROM tan_dictionary WHERE type=2")->result_array();
            $this->template->assign('flag_list', $flag_list);

            if ($plat){
                $plat_info = $this->db->query("SELECT value,name FROM tan_dictionary WHERE type=1 AND value=$plat")->row_array();

                $this->template->assign('plat_name', $plat_info['name']);
            }
            if ($flag){
                $flag_info = $this->db->query("SELECT value,name FROM tan_dictionary WHERE type=2 AND value=$flag")->row_array();
                $this->template->assign('flag_name', $flag_info['name']);
            }

            $this->template->assign('time_start', date('Y-m-d',time()-(6*24*3600)));
            $this->template->assign('time_end', date('Y-m-d',time()));
            $this->template->assign('lend_id', $lend_id);
            $this->template->assign('segment_p',"loanstat");
            $this->template->assign('link', "loanstat_chart.html?lend_id=".$lend_id);
            $this->template->load('statistics/loanstat_chart');
        }


    }
    //单个平台统计记录
    function loanstat_lend(){
        //分页
        $reload_type = $_GET['reload_type']?intval($_GET['reload_type']):0;
        $lend_id = $_GET['lend_id']?intval($_GET['lend_id']):0;
        $flag = $_GET['flag']?intval($_GET['flag']):0;
        $removal = $_GET['removal']?intval($_GET['removal']):0;
        $where = " `type`= $reload_type ";
        if ($lend_id >= 0){
            $where .= " and type_val = $lend_id";
        }
        $group_by = "";
        if ($removal){
            $group_by .= " GROUP BY request_ip ";
        }
        if ($flag){
            $where .= " and request_flag = $flag";
        }

        if (trim($_GET['time-start'])) {
            $time_start = strtotime($_GET['time-start']);
            $where .= " and request_time >= $time_start";
        }
        if (trim($_GET['time-end'])) {
            $time_end = strtotime($_GET['time-end']) + 3600 * 24;
            $where .= " and request_time <= $time_end";

        }

        $getcount = $this->db->query("SELECT * FROM tan_dictionary where type=1")->num_rows();
        $navigation_position = 'outside';
        $records_per_page = 20;
        $this->xpage->navigation_position(isset($navigation_position) && in_array($navigation_position, array('left', 'right')) ? $navigation_position : 'outside');
        //$this->xpage->reverse(true);
        $this->xpage->records($getcount);
        $this->xpage->records_per_page($records_per_page);
        $this->xpage->labels("<<", ">>");
        $flist = $this->db->query("SELECT id,type,name,value FROM tan_dictionary where type = 1 limit ".(($this->xpage->get_page() - 1) * $records_per_page).",".$records_per_page)->result_array();
        $fidmap = "";

        foreach ($flist as $fitem=>$fvalue){
            $fidmap .= $fvalue['value'].',';
            $flist[$fitem]['num'] = $this->db->query("SELECT * FROM tan_reload_log where $where and request_plat='".$fvalue['value'] . "'" . $group_by )->num_rows();
        }
        $list['flist'] = $flist;
        if (trim($fidmap,',')){
            $list['other_num'] = $this->db->query("SELECT * FROM tan_reload_log where $where and request_plat NOT IN (".trim($fidmap,',') .")". $group_by )->num_rows();
        }else{
            $list['other_num'] = $this->db->query("SELECT * FROM tan_reload_log where $where and request_plat = 0 ". $group_by )->num_rows();
        }

        $this->template->assign('reload_type', $reload_type);
        $this->template->assign('lend_id', $lend_id);
        $this->template->assign('removal', $removal);
        $this->template->assign('time_start', trim($_GET['time-start']));
        $this->template->assign('time_end', trim($_GET['time-end']));
        $this->template->assign('Total',$getcount);
        $this->template->assign('showpage',$this->xpage->render(true));
        $this->template->assign('list',$list);
        $this->template->assign('segment_p',"loanstat");
        $this->template->load('statistics/loanstat_lend');
    }
    //平台分类统计
    function loanstat_lend_cat(){
        //分页
        $reload_type = $_GET['reload_type']?intval($_GET['reload_type']):0;
        $lend_id = $_GET['lend_id']?intval($_GET['lend_id']):0;
        $removal = $_GET['removal']?intval($_GET['removal']):0;
        $plat = $_GET['plat']?intval(trim($_GET['plat'])):0;
        $flag = $_GET['flag']?intval(trim($_GET['flag'])):0;
        $where = " `type`= $reload_type ";
        if ($lend_id >= 0){
            $where .= " and type_val = $lend_id";
        }
        if ($plat){
            $where .= " and request_plat=$plat";
        }
        if ($flag){
            $where .= " and request_flag=$flag";
        }

        $group_by = "";
        if ($removal){
            $group_by .= " GROUP BY request_ip ";
        }

        if (trim($_GET['time-start'])) {
            $time_start = strtotime($_GET['time-start']);
            $where .= " and request_time >= $time_start";
        }
        if (trim($_GET['time-end'])) {
            $time_end = strtotime($_GET['time-end']) + 3600 * 24;
            $where .= " and request_time <= $time_end";

        }

        $getcount = $this->db->query("SELECT * FROM tan_reload_log where $where $group_by")->num_rows();
        $navigation_position = 'outside';
        $records_per_page = 10;
        $this->xpage->navigation_position(isset($navigation_position) && in_array($navigation_position, array('left', 'right')) ? $navigation_position : 'outside');
        //$this->xpage->reverse(true);
        $this->xpage->records($getcount);
        $this->xpage->records_per_page($records_per_page);
        $this->xpage->labels("<<", ">>");

        $list = $this->db->query("SELECT * FROM tan_reload_log where $where $group_by ORDER BY id DESC limit ".(($this->xpage->get_page() - 1) * $records_per_page).",".$records_per_page . "")->result_array();

        $flist = $this->db->query("SELECT id,type,name,value FROM tan_dictionary where type = 1")->result_array();
        foreach ($list as $item=>$value){
            $list[$item]['request_time'] = date("Y-m-d H:i:s",$value['request_time']);
            if ($value['type']==1){
                $list[$item]['type_val'] = "跳转";
            }
            if ($value['type']==2){
                $list[$item]['type_val'] = "访问";
            }
            $request_plat = $this->db->query("SELECT id,type,name,value FROM tan_dictionary where `type` = 1 AND `value`=".$value['request_plat'])->row_array();
            $list[$item]['request_plat'] = $request_plat['name'];
            $request_flag = $this->db->query("SELECT id,type,name,value FROM tan_dictionary where `type` = 2 AND `value`=".$value['request_flag'])->row_array();
            $list[$item]['request_flag'] = $request_flag['name'];
        }

        $this->template->assign('reload_type', $reload_type);
        $this->template->assign('lend_id', $lend_id);
        $this->template->assign('removal', $removal);
        $this->template->assign('time_start', trim($_GET['time-start']));
        $this->template->assign('time_end', trim($_GET['time-end']));
        $this->template->assign('Total',$getcount);
        $this->template->assign('showpage',$this->xpage->render(true));
        $this->template->assign('list',$list);
        $this->template->assign('segment_p',"loanstat");
        $this->template->load('statistics/loanstat_lend_cat');
    }

    //定向统计
    function custstat(){
        $where = " 1=1 ";

        $plat = $_GET['plat']?intval(trim($_GET['plat'])):0;
        $flag = $_GET['flag']?intval(trim($_GET['flag'])):0;

        if (trim($_GET['time-start'])) {
            $time_start = strtotime($_GET['time-start']);
            $where .= " and request_time >= $time_start";
        }else{
            $_GET['time-start'] = date('Y-m-d',time());
            $time_start = strtotime($_GET['time-start']);
            $where .= " and request_time >= $time_start";
        }

        if (trim($_GET['time-end'])) {
            $time_end = strtotime($_GET['time-end']) + 3600 * 24;
            $where .= " and request_time <= $time_end";
        }else{
            $_GET['time-end'] = date('Y-m-d',time());
            $time_end = strtotime($_GET['time-end']) + 3600 * 24;
            $where .= " and request_time <= $time_end";
        }

        if ($plat){
            $where .= " AND request_plat = $plat";
        }


        $getcount = $this->db->query("select * from tan_reload_cat WHERE type=3 order by id desc")->num_rows();

        $cat_list = $this->db->query("select * from tan_reload_cat WHERE type=3 order by id desc")->result_array();

        foreach ($cat_list as $cat_item=>$cat){
            $cat_list[$cat_item]['views_num'] = $this->db->query("SELECT request_ip FROM tan_reload_log WHERE $where AND type_val=".$cat['id']." AND type=3")->num_rows();
            $cat_list[$cat_item]['views_num_removal'] = $this->db->query("SELECT request_ip FROM tan_reload_log WHERE $where AND type_val=".$cat['id']." AND type=3 GROUP BY request_ip")->num_rows();
        }
        $list['cat_list'] = $cat_list;

        $plat_list = $this->db->query("SELECT * FROM tan_dictionary WHERE type=1")->result_array();
        $this->template->assign('plat_list', $plat_list);
        $flag_list = $this->db->query("SELECT * FROM tan_dictionary WHERE type=2")->result_array();
        $this->template->assign('flag_list', $flag_list);

        if ($plat>0){
            $plat_info = $this->db->query("SELECT name FROM tan_dictionary WHERE type=1 AND value=$plat")->row_array();
            $this->template->assign('plat_name', $plat_info['name']);
        }
        if ($flag>0){
            $flag_info = $this->db->query("SELECT name FROM tan_dictionary WHERE type=2 AND value=$flag")->row_array();
            $this->template->assign('flag_name', $flag_info['name']);
        }


        $this->template->assign('time_start', trim($_GET['time-start']));
        $this->template->assign('time_end', trim($_GET['time-end']));
        $this->template->assign('Total',$getcount);
        $this->template->assign('showpage',$this->xpage->render(true));
        $this->template->assign('list',$list);
        $this->template->load('statistics/custstat');
    }
    //定向统计图表
    function custstat_chart(){
        //分页
        $cat_id = $_GET['cat_id']?intval($_GET['cat_id']):0;
        $plat = $_GET['plat']?intval($_GET['plat']):0;
        $flag = $_GET['flag']?intval($_GET['flag']):0;

        if ($_POST){
            $where = " 1=1 ";
            if (trim($_POST['time_start'])) {
                $time_start = strtotime($_POST['time_start']);
                $where .= " AND request_time >= $time_start";
            }else{
                $_POST['time_start'] = date('Y-m-d',time()-(6*24*3600));
                $time_start = strtotime($_POST['time-start']);
                $where .= " AND request_time >= $time_start";
            }

            if (trim($_POST['time_end'])) {
                $time_end = strtotime($_POST['time_end']) + 3600 * 24;
                $where .= " AND request_time <= $time_end";
            }else{
                $_POST['time_end'] = date('Y-m-d',time());
                $time_end = strtotime($_POST['time_end']) + 3600 * 24;
                $where .= " AND request_time <= $time_end";
            }
            if (trim($_POST['plat'])){
                $plat = intval(trim($_POST['plat']));
                $where .= " AND request_plat = $plat";
            }
            if (trim($_POST['flag'])){
                $flag = intval(trim($_POST['flag']));
                $where .= " AND request_flag = $flag";
            }

            if ($cat_id > 0){
                $cat_info = $this->db->query("select * from tan_reload_cat WHERE id=$cat_id")->row_array();

                $where .= " AND type=".$cat_info['type']." AND type_val = ".$cat_info['id'];
                $title['text'] = "类型：".$cat_info['title']."-图表";
            }

            $viewnums = $this->db->query("SELECT COUNT(*) AS num, FROM_UNIXTIME(request_time,'%Y-%m-%d') AS days FROM tan_reload_log WHERE $where GROUP BY days")->result_array();
            $viewnums_removal = $this->db->query("SELECT COUNT(DISTINCT request_ip) AS num, FROM_UNIXTIME(request_time,'%Y-%m-%d') AS days FROM tan_reload_log WHERE $where GROUP BY days")->result_array();

            if ($time_start<$time_end){
                $cout_day = ($time_end - $time_start)/86400;
            }else{
                $cout_day = 7;
            }
            $days = array();
            for($i=$cout_day; $i>0; $i--){
                $days[] = date('Y-m-d',$time_end-3600 * 24 * $i);
            }

            foreach ($days as $daykey=>$day){
                if (!$this->deep_in_array($day,$viewnums)){
                    $viewnums[] = array('days'=>$day,'num'=>0);
                }
                if (!$this->deep_in_array($day,$viewnums_removal)){
                    $viewnums_removal[] = array('days'=>$day,'num'=>0);
                }
            }

            $viewnums = $this->sortArrByField($viewnums,'days');
            $viewnums_removal = $this->sortArrByField($viewnums_removal,'days');

            $viewnums_arr = array();
            $viewnums_removal_arr = array();

            foreach ($viewnums as $vitem=>$nval){
                $viewnums_arr[] = $nval['num'];
            }
            foreach ($viewnums_removal as $vritem=>$vrval){
                $viewnums_removal_arr[] = $vrval['num'];
            }

            $option = array(
                'title'     => $title,
                'tooltip'   => array(
                    'trigger'  => 'axis',
                    'axisPointer'=> array(
                        'type'  => 'cross',
                        'label' => array(
                            'backgroundColor'=>'#6a7985'
                        )
                    )
                ),
                'legend'    => array(
                    'data'  => ['点击量','去重点击量']
                ),
                'yAxis'   => [array('type'=>'value')],
                'xAxis'   => [array('type'=>'category','boundaryGap'=>false,'data'=>$days)],
                'series'  => [
                    array(
                        'name'  => '点击量',
                        'type'  => 'line',
                        'stack'  => '总量',
                        'areaStyle'  => array('normal'=>array()),
                        'data'  => $viewnums_arr,
                    ),
                    array(
                        'name'  => '去重点击量',
                        'type'  => 'line',
                        'stack'  => '总量',
                        'areaStyle'  => array('normal'=>array()),
                        'data'  => $viewnums_removal_arr,
                    )
                ]

            );
            echo json_encode(array('option'=>$option),JSON_HEX_TAG);die;
        }else{

            $plat_list = $this->db->query("SELECT * FROM tan_dictionary WHERE type=1")->result_array();
            $this->template->assign('plat_list', $plat_list);

            if ($plat){
                $plat_info = $this->db->query("SELECT name FROM tan_dictionary WHERE type=1 AND value=$plat")->row_array();
                $this->template->assign('plat_name', $plat_info['name']);
            }
            $flag_list = $this->db->query("SELECT * FROM tan_dictionary WHERE type=2")->result_array();
            $this->template->assign('flag_list', $flag_list);

            if ($flag){
                $flag_info = $this->db->query("SELECT name FROM tan_dictionary WHERE type=1 AND value=$flag")->row_array();
                $this->template->assign('flag_name', $flag_info['name']);
            }

            $this->template->assign('time_start', date('Y-m-d',time()-(6*24*3600)));
            $this->template->assign('time_end', date('Y-m-d',time()));
            $this->template->assign('cat_id', $cat_id);
            $this->template->assign('segment_p',"custstat");
            $this->template->assign('link', "custstat_chart.html?cat_id=".$cat_id);
            $this->template->load('statistics/loanstat_chart');
        }

    }

    //来源统计
    function source(){
        //分页
        $flag = $_GET['flag']?intval($_GET['flag']):0;

        $where = " 1=1 ";
        $uwhere = " 1=1 ";
        if (trim($_GET['time-start'])) {
            $time_start = strtotime($_GET['time-start']);
        }else{
            $_GET['time-start'] = date('Y-m-d',time());
            $time_start = strtotime($_GET['time-start']);
        }
        $where .= " and request_time >= $time_start";
        $uwhere .= " and reg_time >= $time_start";

        if (trim($_GET['time-end'])) {
            $time_end = strtotime($_GET['time-end']) + 3600 * 24;
        }else{
            $_GET['time-end'] = date('Y-m-d',time());
            $time_end = strtotime($_GET['time-end']) + 3600 * 24;
        }
        $where .= " and request_time <= $time_end";
        $uwhere .= " and reg_time <= $time_end";

        $getcount = $this->db->query("SELECT * FROM tan_dictionary where type=1")->num_rows();
        $navigation_position = 'outside';
        $records_per_page = 20;
        $this->xpage->navigation_position(isset($navigation_position) && in_array($navigation_position, array('left', 'right')) ? $navigation_position : 'outside');
        //$this->xpage->reverse(true);
        $this->xpage->records($getcount);
        $this->xpage->records_per_page($records_per_page);
        $this->xpage->labels("<<", ">>");
        $flist = $this->db->query("SELECT id,type,name,value FROM tan_dictionary where type = 1 limit ".(($this->xpage->get_page() - 1) * $records_per_page).",".$records_per_page)->result_array();
        $fidmap = "";

        foreach ($flist as $fitem=>$fvalue){
            $fidmap .= $fvalue['value'].',';

            $flist[$fitem]['index_views_num'] = $this->db->query("SELECT request_ip FROM tan_reload_log WHERE $where AND type_val=0 AND request_plat=".$fvalue['value']." AND type=2")->num_rows();
            $flist[$fitem]['index_views_num_removal'] = $this->db->query("SELECT request_ip FROM tan_reload_log WHERE $where AND type_val=0 AND request_plat=".$fvalue['value']." AND type=2 GROUP BY request_ip")->num_rows();

            $flist[$fitem]['views_num'] = $this->db->query("SELECT request_ip FROM tan_reload_log WHERE $where AND type_val>0 AND request_plat=".$fvalue['value']." AND type=2")->num_rows();
            $flist[$fitem]['views_num_removal'] = $this->db->query("SELECT request_ip FROM tan_reload_log WHERE $where AND type_val>0 AND request_plat=".$fvalue['value']." AND type=2 GROUP BY request_ip")->num_rows();

            $flist[$fitem]['jump_num'] = $this->db->query("SELECT request_ip FROM tan_reload_log WHERE $where AND request_plat=".$fvalue['value']." AND type=1")->num_rows();
            $flist[$fitem]['jump_num_removal'] = $this->db->query("SELECT DISTINCT request_ip,type_val FROM tan_reload_log WHERE $where AND request_plat=".$fvalue['value']." AND type=1")->num_rows();

            $flist[$fitem]['flag_regnum'] = $this->db->query("SELECT user_id FROM tan_users WHERE $uwhere AND reg_plat=".$fvalue['value'])->num_rows();
        }

        $list['flist'] = $flist;
//        if (trim($fidmap,',')){
//            $list['other_index_views_num'] = $this->db->query("SELECT * FROM tan_reload_log WHERE $where AND type_val=0 AND request_plat NOT IN (".trim($fidmap,',') .") AND type=2")->num_rows();
//            $list['other_index_views_num_removal'] = $this->db->query("SELECT * FROM tan_reload_log WHERE $where AND type_val=0 AND request_plat NOT IN (".trim($fidmap,',') .") AND type=2 GROUP BY request_ip")->num_rows();
//
//            $list['other_views_num'] = $this->db->query("SELECT * FROM tan_reload_log WHERE $where AND type_val>0 AND request_plat NOT IN (".trim($fidmap,',') .") AND type=2")->num_rows();
//            $list['other_views_num_removal'] = $this->db->query("SELECT * FROM tan_reload_log WHERE $where AND type_val>0 and request_plat NOT IN (".trim($fidmap,',') .") AND type=2 GROUP BY request_ip")->num_rows();
//            $list['other_jump_num'] = $this->db->query("SELECT * FROM tan_reload_log WHERE $where AND type_val>0 AND request_plat NOT IN (".trim($fidmap,',') .") AND type=1")->num_rows();
//            $list['other_jump_num_removal'] = $this->db->query("SELECT * FROM tan_reload_log WHERE $where AND type_val>0 AND request_plat NOT IN (".trim($fidmap,',') .") AND type=1 GROUP BY request_ip")->num_rows();
//            $list['other_flag_regnum'] = $this->db->query("SELECT user_id FROM tan_users WHERE $uwhere AND reg_plat NOT IN(".trim($fidmap,',').")" )->num_rows();
//        }else{
//            $list['other_index_views_num'] = $this->db->query("SELECT * FROM tan_reload_log WHERE $where AND type_val=0 AND request_plat = 0 AND type=2")->num_rows();
//            $list['other_index_views_num_removal'] = $this->db->query("SELECT * FROM tan_reload_log WHERE $where AND type_val=0 AND request_plat = 0 AND type=2 GROUP BY request_ip")->num_rows();
//
//            $list['other_views_num'] = $this->db->query("SELECT * FROM tan_reload_log WHERE $where AND type_val>0 AND request_plat = 0 AND type=2")->num_rows();
//            $list['other_views_num_removal'] = $this->db->query("SELECT * FROM tan_reload_log WHERE $where AND type_val>0 AND request_plat = 0 AND type=2 GROUP BY request_ip")->num_rows();
//            $list['other_jump_num'] = $this->db->query("SELECT * FROM tan_reload_log WHERE $where AND type_val>0 AND request_plat = 0 AND type=1")->num_rows();
//            $list['other_jump_num_removal'] = $this->db->query("SELECT * FROM tan_reload_log WHERE $where AND type_val>0 AND request_plat = 0 AND type=1 GROUP BY request_ip")->num_rows();
//            $list['other_flag_regnum'] = $this->db->query("SELECT user_id FROM tan_users WHERE $uwhere AND reg_plat NOT IN(".trim($fidmap,',').")" )->num_rows();
//        }


        $this->template->assign('time_start', trim($_GET['time-start']));
        $this->template->assign('time_end', trim($_GET['time-end']));
        $this->template->assign('Total',$getcount);
        $this->template->assign('showpage',$this->xpage->render(true));
        $this->template->assign('list',$list);
        $this->template->assign('segment_p',"source");
        $this->template->load('statistics/source');
    }
    //来源注册管理
    function platreg(){
        //分页
        //$flag = $_GET['flag']?intval($_GET['flag']):0;

        $_GET['time-start'] = $_GET['time-start'] ? trim($_GET['time-start']) : date('Y-m-d',time());
        $_GET['time-end'] = $_GET['time-end'] ? trim($_GET['time-end']) : date('Y-m-d',time());

        $time_start = strtotime($_GET['time-start']);
        $time_end   = strtotime($_GET['time-end']);

        $sql = "select * from tan_dictionary where type=1 and value>99";
        $getcount = $this->db->query("$sql")->num_rows();
        $navigation_position = 'outside';
        $records_per_page = 20;
        $this->xpage->navigation_position(isset($navigation_position) && in_array($navigation_position, array('left', 'right')) ? $navigation_position : 'outside');
        //$this->xpage->reverse(true);
        $this->xpage->records($getcount);
        $this->xpage->records_per_page($records_per_page);
        $this->xpage->labels("<<", ">>");
        $reg_log_list = $this->db->query("{$sql} limit ".(($this->xpage->get_page() - 1) * $records_per_page).",".$records_per_page)->result_array();

        $today = date('Y-m-d',time());
        if (date('Y-m-d',$time_start)<>$today || date('Y-m-d',$time_end)<>$today){
            $screen = 1;
            $this->template->assign('screen',$screen);
        }
        foreach ($reg_log_list as $key=>$val){

            $list = $this->statistics_model->get_platreg_list($val['id'],$time_start,$time_end);
            foreach ($list as $list_key=>$list_val){
                $reg_log_list[$key]['dic_id'] = $list_val['dic_id'];
                $reg_log_list[$key]['name'] = $list_val['name'];
                $reg_log_list[$key]['link'] = "http://".$_SERVER['HTTP_HOST']."/statistics.html?plat=".$list_val['value'];
                $reg_log_list[$key]['real_num'] += $list_val['real_num'];
                $reg_log_list[$key]['percent_num'] += $list_val['percent_num'];
                $reg_log_list[$key]['percent'] = $list_val['percent'];
            }
        }

        $this->template->assign('time_start', trim($_GET['time-start']));
        $this->template->assign('time_end', trim($_GET['time-end']));
        $this->template->assign('Total',$getcount);
        $this->template->assign('showpage',$this->xpage->render(true));
        $this->template->assign('list',$reg_log_list);
        $this->template->assign('segment_p',"platreg");
        $this->template->load('statistics/platreg');
    }
    function platreg_edit(){

        if($_POST){
            $id = trim($_POST['id']);

            if(!trim($_POST['percent'])){
                $_POST['percent']= 0;
            }
            $today = date("Y-m-d", time());
            $percent = trim($_POST['percent']);
            $platreg_log = $this->db->query("select * from tan_platreg_log where dic_id=$id and log_time='$today'")->row_array();
            $platreg_val = $this->statistics_model->get_platreg_day($id,$today);
            if ($platreg_log){
                $data = array(
                    'percent'=>$percent,
                    'real_num'=>$platreg_val['real_num'],
                    'per_num'=>$platreg_val['percent_num'],
                );
                $result = $this->db->where(array('dic_id'=>$id,'log_time'=>$today))->update('platreg_log', $data);
            }else{
                $data = array(
                    'dic_id' => $id,
                    'percent' => $percent,
                    'real_num' => $platreg_val['real_num'],
                    'per_num' => $platreg_val['percent_num'],
                    'log_time' => $platreg_val['day']
                );
                $result = $this->db->insert('platreg_log', $data);
            }

            if($result){
                $logdata = array();
                $logdata['user_id'] = $this->userinfo['id'];
                $logdata['username'] = $this->userinfo['username'];
                $logdata['event'] = 'system';
                $logdata['content'] = '修改定制统计信息：'.$_POST['title'].'-'.$_POST['url'];
                $this->admin_log_model->insert($logdata);


                $mssage['info']	  = '修改成功';
                $mssage['status'] = 1;
                $mssage['url']	  = site_url('statistics/platreg');
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '操作失败';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }
        }
    }
    //来源注册管理
    function platreg_list(){
        //分页
        //$flag = $_GET['flag']?intval($_GET['flag']):0;

        $_GET['time-start'] = $_GET['time-start'] ? trim($_GET['time-start']) : date('Y-m-d',time());
        $_GET['time-end'] = $_GET['time-end'] ? trim($_GET['time-end']) : date('Y-m-d',time());
        $plat['val'] = $_GET['val'] ? intval(trim($_GET['val'])) : 0;
        $plat['id'] = $_GET['plat_id'] ? intval(trim($_GET['plat_id'])) : 0;

        $time_start = strtotime($_GET['time-start']);
        $time_end   = strtotime($_GET['time-end'])+3600*24;
        $where = " 1=1 ";
        if ($plat['val']){
            $where .= " AND reg_plat = ".$plat['val'];
        }
        if ($time_start){
            $where .= " AND reg_time >= $time_start";
        }
        if ($time_end){
            $where .= " AND reg_time <= $time_end";
        }
        $user_sql = "select username,mobile,email,reg_time,reg_ip,login_times from tan_users where $where";

        $getcount = $this->db->query("$user_sql")->num_rows();
        $navigation_position = 'outside';
        $records_per_page = 20;
        $this->xpage->navigation_position(isset($navigation_position) && in_array($navigation_position, array('left', 'right')) ? $navigation_position : 'outside');
        //$this->xpage->reverse(true);
        $this->xpage->records($getcount);
        $this->xpage->records_per_page($records_per_page);
        $this->xpage->labels("<<", ">>");
        $reg_list = $this->db->query("{$user_sql} limit ".(($this->xpage->get_page() - 1) * $records_per_page).",".$records_per_page)->result_array();

        foreach ($reg_list as $key=>$val){
            $reg_list[$key]['reg_time'] = date('Y-m-d H:i:s',$val['reg_time']);
        }

        $this->template->assign('time_start', trim($_GET['time-start']));
        $this->template->assign('time_end', trim($_GET['time-end']));

        $this->template->assign('plat',$plat);
        $this->template->assign('Total',$getcount);
        $this->template->assign('showpage',$this->xpage->render(true));
        $this->template->assign('list',$reg_list);
        $this->template->assign('segment_p',"platreg");
        $this->template->load('statistics/platreg_list');
    }

    public function platreg_excel(){
        $time_start = $_GET['time-start'] ? trim($_GET['time-start']) : date('Y-m-d',time());
        $time_end = $_GET['time-end'] ? trim($_GET['time-end']) : date('Y-m-d',time());
        $plat['val'] = $_GET['val'] ? intval(trim($_GET['val'])) : 0;
        $plat['id'] = $_GET['plat_id'] ? intval(trim($_GET['plat_id'])) : 0;

        $dic_info = $this->db->query("SELECT * FROM tan_dictionary WHERE id=".$plat['id'])->row_array();

        $time_start = strtotime($time_start);
        $time_end   = strtotime($time_end)+3600*24;
        $where = " 1=1 ";
        if ($plat['val']){
            $where .= " AND reg_plat = ".$plat['val'];
        }
        if ($time_start){
            $where .= " AND reg_time >= $time_start";
        }
        if ($time_end){
            $where .= " AND reg_time <= $time_end";
        }
        $user_sql = "select username,mobile,email,reg_time,reg_ip,login_times from tan_users where $where";

        $reg_list = $this->db->query("{$user_sql}")->result_array();

        $ListRowData = array();

        foreach ($reg_list as $key=>$val){
            $temp_arr = array();
            $temp_arr[] = $val['username'];
            $temp_arr[] = $val['mobile'];
            $temp_arr[] = $val['emial'];
            $temp_arr[] = date('Y-m-d H:i:s',$val['reg_time']);
            $temp_arr[] = $val['reg_ip'];
            $temp_arr[] = $val['login_times'];
            $ListRowData[$key]['RowData'] = $temp_arr;
        }

        $this->load->library('Export');
        $HeadData = array('用户名','手机号','E-mail','注册时间','注册IP','登录次数');
        $filename = $dic_info['name'].'-注册记录（'.$_GET['time-start'].'-'.$_GET['time-end'].'）';
        echo $this->export->users($HeadData,$ListRowData,$filename);exit;
    }
    //来源统计图表
    function source_chart(){
        $plat = $_GET['plat']?intval($_GET['plat']):0;
        $flag = $_GET['flag']?intval($_GET['flag']):0;
        //分页
        if ($_POST){
            $where = " 1=1 ";
            $uwhere = " 1=1 ";
            if (trim($_POST['time_start'])) {
                $time_start = strtotime($_POST['time_start']);
                $where .= " AND request_time >= $time_start";
                $uwhere .= " and reg_time >= $time_start";
            }else{
                $_POST['time_start'] = date('Y-m-d',time()-(6*24*3600));
                $time_start = strtotime($_POST['time-start']);
                $where .= " AND request_time >= $time_start";
                $uwhere .= " and reg_time >= $time_start";
            }

            if (trim($_POST['time_end'])) {
                $time_end = strtotime($_POST['time_end']) + 3600 * 24;
                $where .= " AND request_time <= $time_end";
                $uwhere .= " and reg_time <= $time_end";
            }else{
                $_POST['time_end'] = date('Y-m-d',time());
                $time_end = strtotime($_POST['time_end']) + 3600 * 24;
                $where .= " AND request_time <= $time_end";
                $uwhere .= " and reg_time <= $time_end";
            }
            if ($time_start<$time_end){
                $cout_day = ($time_end - $time_start)/86400;
            }else{
                $cout_day = 7;
            }

            $days = array();
            for($i=$cout_day; $i>0; $i--){
                $days[] = date('Y-m-d',$time_end-3600 * 24 * $i);
            }

            if ($plat > 0){
                $plat_info = $this->db->query("select * from tan_dictionary WHERE type=1 AND value=$plat")->row_array();

                $where .= " AND request_plat = ".$plat_info['value'];
                $uwhere .= " AND reg_plat = ".$plat_info['value'];
                $title['text'] = "来源：".$plat_info['name']."-统计图表";
            }
            if ($flag){
                $where .= " AND request_flag = $flag";
            }

            $index_viewnums = $this->db->query("SELECT COUNT(*) AS num, FROM_UNIXTIME(request_time,'%Y-%m-%d') AS days FROM tan_reload_log WHERE $where AND type_val=0 AND type=2 GROUP BY days")->result_array();
            $index_viewnums_removal = $this->db->query("SELECT COUNT(DISTINCT request_ip) AS num, FROM_UNIXTIME(request_time,'%Y-%m-%d') AS days FROM tan_reload_log WHERE $where AND type_val=0 AND type=2 GROUP BY days")->result_array();

            $viewnums = $this->db->query("SELECT COUNT(*) AS num, FROM_UNIXTIME(request_time,'%Y-%m-%d') AS days FROM tan_reload_log WHERE $where AND type_val>0 AND type=2 GROUP BY days")->result_array();
            $viewnums_removal = $this->db->query("SELECT COUNT(DISTINCT request_ip) AS num, FROM_UNIXTIME(request_time,'%Y-%m-%d') AS days FROM tan_reload_log WHERE $where AND type_val>0 AND type=2 GROUP BY days")->result_array();
            $jumpnums = $this->db->query("SELECT COUNT(*) AS num, FROM_UNIXTIME(request_time,'%Y-%m-%d') AS days FROM tan_reload_log WHERE $where AND type_val>0 AND type=1 GROUP BY days")->result_array();
            $jumpnums_removal = $this->db->query("SELECT COUNT(DISTINCT request_ip) AS num, FROM_UNIXTIME(request_time,'%Y-%m-%d') AS days FROM tan_reload_log WHERE $where AND type_val>0 AND type=1 GROUP BY days")->result_array();

            $plat_regnum = $this->db->query("SELECT COUNT(user_id) AS num, FROM_UNIXTIME(reg_time,'%Y-%m-%d') AS days FROM tan_users WHERE $uwhere GROUP BY days")->result_array();

            $index_viewnums_arr = array();
            $index_viewnums_removal_arr = array();
            $viewnums_arr = array();
            $viewnums_removal_arr = array();
            $jumpnums_arr = array();
            $jumpnums_removal_arr = array();
            $plat_regnum_arr = array();

            foreach ($days as $daykey=>$day){
                if (!$this->deep_in_array($day,$index_viewnums)){
                    $index_viewnums[] = array('days'=>$day,'num'=>0);
                }
                if (!$this->deep_in_array($day,$index_viewnums_removal)){
                    $index_viewnums_removal[] = array('days'=>$day,'num'=>0);
                }
                if (!$this->deep_in_array($day,$viewnums)){
                    $viewnums[] = array('days'=>$day,'num'=>0);
                }
                if (!$this->deep_in_array($day,$viewnums_removal)){
                    $viewnums_removal[] = array('days'=>$day,'num'=>0);
                }
                if (!$this->deep_in_array($day,$jumpnums)){
                    $jumpnums[] = array('days'=>$day,'num'=>0);
                }
                if (!$this->deep_in_array($day,$jumpnums_removal)){
                    $jumpnums_removal[] = array('days'=>$day,'num'=>0);
                }
                if (!$this->deep_in_array($day,$plat_regnum)){
                    $plat_regnum[] = array('days'=>$day,'num'=>0);
                }
            }

            $viewnums = $this->sortArrByField($viewnums,'days');
            $viewnums_removal = $this->sortArrByField($viewnums_removal,'days');
            $index_viewnums = $this->sortArrByField($index_viewnums,'days');
            $index_viewnums_removal = $this->sortArrByField($index_viewnums_removal,'days');
            $jumpnums = $this->sortArrByField($jumpnums,'days');
            $jumpnums_removal = $this->sortArrByField($jumpnums_removal,'days');
            $plat_regnum = $this->sortArrByField($plat_regnum,'days');

            foreach ($viewnums as $vitem=>$nval){
                $viewnums_arr[] = $nval['num'];
            }
            foreach ($viewnums_removal as $vritem=>$vrval){
                $viewnums_removal_arr[] = $vrval['num'];
            }
            foreach ($index_viewnums as $ivitem=>$inval){
                $index_viewnums_arr[] = $inval['num'];
            }
            foreach ($index_viewnums_removal as $ivritem=>$ivrval){
                $index_viewnums_removal_arr[] = $ivrval['num'];
            }
            foreach ($jumpnums as $jntem=>$jnval){
                $jumpnums_arr[] = $jnval['num'];
            }
            foreach ($jumpnums_removal as $jrtem=>$jrval){
                $jumpnums_removal_arr[] = $jrval['num'];
            }
            foreach ($plat_regnum as $prtem=>$prval){
                $plat_regnum_arr[] = $prval['num'];
            }

            $option = array(
                'title'     => $title,
                'tooltip'   => array(
                    'trigger'  => 'axis',
                    'axisPointer'=> array(
                        'type'  => 'cross',
                        'label' => array(
                            'backgroundColor'=>'#6a7985'
                        )
                    )
                ),
                'legend'    => array(
                    'data'  => ['首页浏览量','首页去重浏览量','二级页浏览量','二级页去重浏览量','跳转量','去重跳转量','注册量']
                ),
                'yAxis'   => [array('type'=>'value')],
                'xAxis'   => [array('type'=>'category','boundaryGap'=>false,'data'=>$days)],
                'series'  => [
                    array(
                        'name'  => '首页浏览量',
                        'type'  => 'line',
                        'stack'  => '总量',
                        'areaStyle'  => array('normal'=>array()),
                        'data'  => $index_viewnums_arr,
                    ),
                    array(
                        'name'  => '首页去重浏览量',
                        'type'  => 'line',
                        'stack'  => '总量',
                        'areaStyle'  => array('normal'=>array()),
                        'data'  => $index_viewnums_removal_arr,
                    ),
                    array(
                        'name'  => '二级页浏览量',
                        'type'  => 'line',
                        'stack'  => '总量',
                        'areaStyle'  => array('normal'=>array()),
                        'data'  => $viewnums_arr,
                    ),
                    array(
                        'name'  => '二级页去重浏览量',
                        'type'  => 'line',
                        'stack'  => '总量',
                        'areaStyle'  => array('normal'=>array()),
                        'data'  => $viewnums_removal_arr,
                    ),
                    array(
                        'name'  => '跳转量',
                        'type'  => 'line',
                        'stack'  => '总量',
                        'areaStyle'  => array('normal'=>array()),
                        'data'  => $jumpnums_arr,
                    ),
                    array(
                        'name'  => '去重跳转量',
                        'type'  => 'line',
                        'stack'  => '总量',
                        'areaStyle'  => array('normal'=>array()),
                        'data'  => $jumpnums_removal_arr,
                    ),
                    array(
                        'name'  => '注册量',
                        'type'  => 'line',
                        'stack'  => '总量',
                        'areaStyle'  => array('normal'=>array()),
                        'data'  => $plat_regnum_arr,
                    ),
                ]

            );
            echo json_encode(array('option'=>$option),JSON_HEX_TAG);die;
        }else{

            $plat_list = $this->db->query("SELECT * FROM tan_dictionary WHERE type=1")->result_array();
            $this->template->assign('plat_list', $plat_list);

            if ($plat){
                $plat_info = $this->db->query("SELECT name FROM tan_dictionary WHERE type=1 AND value=$plat")->row_array();
                $this->template->assign('plat_name', $plat_info['name']);
            }
            $flag_list = $this->db->query("SELECT * FROM tan_dictionary WHERE type=2")->result_array();
            $this->template->assign('flag_list', $flag_list);

            if ($flag){
                $flag_info = $this->db->query("SELECT name FROM tan_dictionary WHERE type=1 AND value=$flag")->row_array();
                $this->template->assign('flag_name', $flag_info['name']);
            }

            $this->template->assign('time_start', date('Y-m-d',time()-(6*24*3600)));
            $this->template->assign('time_end', date('Y-m-d',time()));

            $this->template->assign('segment_p',"source");
            $this->template->assign('link', "source_chart.html?plat=".$plat);
            $this->template->load('statistics/loanstat_chart');
        }

    }
    //定向信息管理
    function custinfo(){

        $where = array();
        //分页
        $getcount = $this->db->query("select * from tan_reload_cat WHERE type=3 order by id desc")->num_rows();

        $navigation_position = 'outside';
        $records_per_page = 10;
        $this->xpage->navigation_position(isset($navigation_position) && in_array($navigation_position, array('left', 'right')) ? $navigation_position : 'outside');
        $this->xpage->records($getcount);
        $this->xpage->records_per_page($records_per_page);
        $this->xpage->labels("<<", ">>");

        $cat_list = $this->db->query("SELECT * FROM tan_reload_cat where type = 3 limit ".(($this->xpage->get_page() - 1) * $records_per_page).",".$records_per_page)->result_array();

        foreach ($cat_list as $item=>$value){
            $cat_list[$item]['creat_time'] = date('Y-m-d H:i:s',$value['creat_time']);
        }

        //重定义页面
        $this->template->assign('Total',$getcount);
        $this->template->assign('showpage',$this->xpage->render(true));
        $this->template->assign('list',$cat_list);
        $this->template->load('statistics/custinfo');
    }
    //定向信息新增
    function custinfo_add(){
        if($_POST){
            $data = array();
            if(!trim($_POST['title'])){
                $mssage['info']	  = '名称必须输入';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }

            if(!trim($_POST['url'])){
                $mssage['info']	  = '目标URL必须填写';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }


            $data['title'] = trim($_POST['title']);
            $data['url'] = trim($_POST['url']);
            $data['type'] = 3;
            $data['creat_time'] = time();
            $result = $this->db->insert('reload_cat', $data);
            if($result){
                $logdata = array();
                $logdata['user_id'] = $this->userinfo['id'];
                $logdata['username'] = $this->userinfo['username'];
                $logdata['event'] = 'system';
                $logdata['content'] = '增加定制统计信息：'.$_POST['title'].'-'.$_POST['url'];
                $this->admin_log_model->insert($logdata);


                $mssage['info']	  = '增加成功';
                $mssage['status'] = 1;
                $mssage['url']	  = site_url('statistics/custinfo');
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '新增不成功';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }

        }
        $this->template->assign('act_t','新增');
        $this->template->load('statistics/custinfo_info');
    }
    //编辑定向信息
    function custinfo_edit(){
        $id = intval($_GET['id']);
        if(!$id){
            redirect('statistics/custinfo');
        }

        if($_POST){
            $id = trim($_POST['id']);
            $data = array();
            if(!trim($_POST['title'])){
                $mssage['info']	  = '名称必须输入';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }

            if(!trim($_POST['url'])){
                $mssage['info']	  = '目标URL必须填写';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }


            $data['title'] = trim($_POST['title']);
            $data['url'] = trim($_POST['url']);

            $result = $this->db->where('id',$id)->update('reload_cat', $data);
            if($result){
                $logdata = array();
                $logdata['user_id'] = $this->userinfo['id'];
                $logdata['username'] = $this->userinfo['username'];
                $logdata['event'] = 'system';
                $logdata['content'] = '修改定制统计信息：'.$_POST['title'].'-'.$_POST['url'];
                $this->admin_log_model->insert($logdata);


                $mssage['info']	  = '修改成功';
                $mssage['status'] = 1;
                $mssage['url']	  = site_url('statistics/custinfo');
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '操作失败';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }
        }

        $item = $this->db->query("select * from tan_reload_cat WHERE type=3 AND id=$id")->row_array();

        $this->template->assign('act_t','修改');
        $this->template->assign('item',$item);
        $this->template->load('statistics/custinfo_info');
    }
    //删除定向信息
    function custinfo_del()
    {
        $id	   = intval($_GET['id']);
        if(!$id){
            redirect('statistics/custinfo');
        }

        $result = $this->dictionary_model->delete($id);
        if($result){
            $logdata = array();
            $logdata['user_id'] = $this->userinfo['id'];
            $logdata['username'] = $this->userinfo['username'];
            $logdata['event'] = 'dictionary';
            $logdata['content'] = '删除定制统计：【'.$_GET['name'].'】';
            $this->admin_log_model->insert($logdata);

            $mssage['info']	  = '删除成功';
            $mssage['status'] = 1;
            $mssage['url']	  = $_SERVER['HTTP_REFERER'];
            echo json_encode($mssage);exit;
        }else{
            $mssage['info']	  = '删除失败';
            $mssage['status'] = 0;
            $mssage['url']	  = $_SERVER['HTTP_REFERER'];
            echo json_encode($mssage);exit;
        }

    }

    function deep_in_array($value, $array) {
        foreach($array as $item) {
            if(!is_array($item)) {
                if ($item == $value) {
                    return true;
                } else {
                    continue;
                }
            }

            if(in_array($value, $item)) {
                return true;
            } else if($this->deep_in_array($value, $item)) {
                return true;
            }
        }
        return false;
    }

    //二维数组按字段排序
    function sortArrByField($array, $field, $desc = false){
        $fieldArr = array();
        foreach ($array as $k => $v) {
            $fieldArr[$k] = $v[$field];
        }
        $sort = $desc == false ? SORT_ASC : SORT_DESC;
        array_multisort($fieldArr, $sort, $array);
        return $array;
    }

}
